package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.comm.ZOZOPOCODE;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.flow.operations.FlowTB;
import zozopo.flow.operations.RecordlistTB;
import zozopo.operations.comm.DictionaryOS;
import zozopo.operations.comm.OperatingService;
import zozopo.user.operations.UserTB;

public class PurchaseOS extends OperatingService{
	public PurchaseOS(Connection conn){
		super(conn);
	}
	
	public PurchaseOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<PurchaseTB> selectListBySql(String sql){
		List<PurchaseTB> purchaseList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		System.out.println(sql);
		//DictionaryOS dictionaryOS = new DictionaryOS(conn);
		if(resultSet != null){
				try {
					purchaseList = new ArrayList<PurchaseTB>();
					while(resultSet.next()){
						PurchaseTB purchase = new PurchaseTB();
						
						
						
						purchase.setDate_purchase(resultSet.getString(PurchaseTB.DATE_PURCHASE));
						purchase.setFwcnspell_purchase(resultSet.getString(PurchaseTB.FWCNSPELL_PURCHASE));
						purchase.setGoodsid_purchase(resultSet.getInt(PurchaseTB.GOODSID_PURCHASE));
						purchase.setGoodsname_purchase(resultSet.getString(PurchaseTB.GOODSNAME_PURCHASE));
						purchase.setGoodunit_purchase(resultSet.getString(PurchaseTB.GOODUNIT_PURCHASE));
						purchase.setId_purchase(resultSet.getInt(PurchaseTB.ID_PURCHASE));
						purchase.setPurchaselistid_purchase(resultSet.getInt(PurchaseTB.PURCHASELISTID_PURCHASE));
						purchase.setBackreceiptlistid_purchase(resultSet.getInt(PurchaseTB.BACKRECEIPTLISTID_PURCHASE));
						
						//purchase.setKind_purchase(resultSet.getString(PurchaseTB.KIND_PURCHASE));
						//purchase.setLable_purchase(resultSet.getString(PurchaseTB.LABLE_PURCHASE));
						
						purchase.setNumber_purchase(resultSet.getInt(PurchaseTB.NUMBER_PURCHASE));
						
						purchase.setReserve_purchase(resultSet.getInt(PurchaseTB.RESERVE_PURCHASE));
						purchase.setPrice_purchase(resultSet.getDouble(PurchaseTB.PRICE_PURCHASE));
						purchase.setTotal_purchase(resultSet.getDouble(PurchaseTB.TOTAL_PURCHASE));
						
						purchase.setStock_purchase(resultSet.getInt(PurchaseTB.STOCK_PURCHASE));
						
						//purchase.setAverage_goods(resultSet.getDouble(PurchaseTB.AVERAGE_PURCHASE));
						
						purchase.setRemark_purchase(resultSet.getString(PurchaseTB.REMARK_PURCHASE));
						purchase.setSupplierid_purchase(resultSet.getInt(PurchaseTB.SUPPLIERID_PURCHASE));
						purchase.setSuppliername_purchase(resultSet.getString(PurchaseTB.SUPPLIERNAME_PURCHASE));
						purchase.setUseremail_purchase(resultSet.getString(PurchaseTB.USEREMAIL_PURCHASE));
						purchase.setUsername_purchase(resultSet.getString(PurchaseTB.USERNAME_PURCHASE));
						purchase.setWarehouseid_purchase(resultSet.getInt(PurchaseTB.WAREHOUSEID_PURCHASE));
						purchase.setWarehousename_purchase(resultSet.getString(PurchaseTB.WAREHOUSENAME_PURCHASE));
												
						
						purchase.setIscancel_purchase(resultSet.getBoolean(PurchaseTB.ISCANCEL_PURCHASE));
						purchase.setIsconfirm_purchase(resultSet.getBoolean(PurchaseTB.ISCONFIRM_PURCHASE));
						
					//	purchase.setCnnamediply(dictionaryOS.getENNameByKindAndValue("WAREHOUSE.PURCHASE.STATE", purchase.getState_purchase()).getDictionary_cnname());
						
						purchaseList.add(purchase);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return purchaseList;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(PurchaseTB purchase,boolean isConfirm,boolean isSystem){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		GoodsOS goodsOS = new GoodsOS(conn);
		
		GoodsTB goods = goodsOS.getGoodsById(purchase.getGoodsid_purchase());
		
		
		insertMap.put(PurchaseTB.GOODSID_PURCHASE, goods.getId_goods());
		
		insertMap.put(PurchaseTB.GOODSNAME_PURCHASE, goods.getName_goods());
		insertMap.put(PurchaseTB.CNSPELL_PURCHASE, goods.getCnspell_goods()+"");
		insertMap.put(PurchaseTB.FWCNSPELL_PURCHASE, goods.getFwcnspell_goods()+"");
		insertMap.put(PurchaseTB.GOODUNIT_PURCHASE, goods.getUnit_goods());
		
	
		insertMap.put(PurchaseTB.WAREHOUSEID_PURCHASE, purchase.getWarehouseid_purchase());
		insertMap.put(PurchaseTB.WAREHOUSENAME_PURCHASE, purchase.getWarehousename_purchase());
		
		insertMap.put(PurchaseTB.SUPPLIERID_PURCHASE, purchase.getSupplierid_purchase());
		insertMap.put(PurchaseTB.SUPPLIERNAME_PURCHASE, purchase.getSuppliername_purchase());
		
		
		insertMap.put(PurchaseTB.BACKRECEIPTLISTID_PURCHASE, purchase.getBackreceiptlistid_purchase());
		
		insertMap.put(PurchaseTB.PRICE_PURCHASE, purchase.getPrice_purchase());
		insertMap.put(PurchaseTB.NUMBER_PURCHASE, purchase.getNumber_purchase());
		insertMap.put(PurchaseTB.RESERVE_PURCHASE, purchase.getNumber_purchase());
		
		insertMap.put(PurchaseTB.TOTAL_PURCHASE, purchase.getPrice_purchase()*purchase.getNumber_purchase());
		
		insertMap.put(PurchaseTB.REMARK_PURCHASE, purchase.getRemark_purchase());
		insertMap.put(PurchaseTB.PURCHASELISTID_PURCHASE, purchase.getPurchaselistid_purchase());
		
		insertMap.put(PurchaseTB.USEREMAIL_PURCHASE, purchase.getUseremail_purchase());
		insertMap.put(PurchaseTB.USERNAME_PURCHASE, purchase.getUsername_purchase());
		
		insertMap.put(PurchaseTB.ISCONFIRM_PURCHASE, isConfirm);
		insertMap.put(PurchaseTB.ISSYSTEM_PURCHASE, isSystem);
		
		
		
		sqlMachine.insertRow(PurchaseTB.TABLE_NAME, insertMap);
			
		
		return updateGoodsAttribute(purchase.getGoodsid_purchase(),purchase.getUseremail_purchase());
	}
	
	
	
	
	public boolean insert(List<PurchaseTB> purchaseInfoList,boolean isConfirm,boolean isSystem){
		for(PurchaseTB obj:purchaseInfoList){
			if(!insert(obj,isConfirm,isSystem)){
				return false;
			}
		}
		
		return true;
	}
	
	
	
	public boolean updateGoodsAttribute(int id,String usermail){
		
		GoodsOS goodsOS = new GoodsOS(conn);
		GoodsTB goods = goodsOS.getGoodsById(id); 
		int sumNum = sumGoodsNumberById(goods.getId_goods());  
		double sumCountNum = sumCountGoodsNumberById(goods.getId_goods());
		//double average = averageGoodsyId(goods.getId_goods());
		
		Double average = sumCountNum / sumNum;
		
		if(average.isNaN()){
			average = 0.00;
		}
		
		
		DecimalFormat   myFormatter   =   new   DecimalFormat("##################.##");  
		  //注意,这句整体作为String
		Double formatAvg = Double.valueOf(myFormatter.format(average)); 
		double newPrice = getNewPrice(id,usermail);
		
		
		return goodsOS.upadteGoodsAverageAndNumById(goods.getId_goods(),sumNum,formatAvg,newPrice);
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean confirm(List<PurchaseTB> purchaseInfoList){
		for(PurchaseTB obj:purchaseInfoList){
			if(!confirm(obj)){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean unconfirm(List<PurchaseTB> purchaseInfoList){
		for(PurchaseTB obj:purchaseInfoList){
			if(!unconfirm(obj)){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean unconfirm(PurchaseTB purchase){
		SqlMachine sqlMachine = new SqlMachine(conn);
		
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(PurchaseTB.ISCONFIRM_PURCHASE, false);
		//updateMap.put(PurchaseTB.STATE_PURCHASE, ZOZOPOCODE.LOCKED);
		
		sqlMachine.updateRow(PurchaseTB.TABLE_NAME, updateMap, PurchaseTB.ID_PURCHASE, purchase.getId_purchase());
		
		return updateGoodsAttribute(purchase.getGoodsid_purchase(),purchase.getUseremail_purchase());
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	public boolean confirm(PurchaseTB purchase){
		SqlMachine sqlMachine = new SqlMachine(conn);
		
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		updateMap.put(PurchaseTB.ISCONFIRM_PURCHASE, true);
		
		sqlMachine.updateRow(PurchaseTB.TABLE_NAME, updateMap, PurchaseTB.ID_PURCHASE, purchase.getId_purchase());
		
		return updateGoodsAttribute(purchase.getGoodsid_purchase(),purchase.getUseremail_purchase());
	}
	
	public boolean updateGoodsAttribute(int id){
		
		GoodsOS goodsOS = new GoodsOS(conn);
		GoodsTB goods = goodsOS.getGoodsById(id); 
		int sumNum = sumGoodsNumberById(goods.getId_goods());
		double sumCountNum = sumCountGoodsNumberById(goods.getId_goods());
		//double average = averageGoodsyId(goods.getId_goods());
		
		Double average = sumCountNum / sumNum;
		
		if(average.isNaN()){
			average = 0.00;
		}
		
		DecimalFormat   myFormatter   =   new   DecimalFormat("##################.##");  
		  //注意,这句整体作为String
		Double formatAvg = Double.valueOf(myFormatter.format(average)); 
		
		
		
		return goodsOS.upadteGoodsAverageAndNumById(goods.getId_goods(),sumNum,formatAvg);
	}
	

	/**
	 * @version 1.0
	 * @author Shea
	 */
	public double getNewPrice(int id,String usermail){
		double newPrice = 0;
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME + " where " + PurchaseTB.USEREMAIL_PURCHASE + " ='"+usermail+"'"
		+ " and " + PurchaseTB.ISCANCEL_PURCHASE + "=false" 
		+" and " + PurchaseTB.RESERVE_PURCHASE +"!=0"
		+" and " + PurchaseTB.ISSYSTEM_PURCHASE +"=false "
		+" ORDER BY " + PurchaseTB.DATE_PURCHASE + " DESC "
		+" limit "+0+","+1;
		List<PurchaseTB> purchaseList = selectListBySql(sql);
		for(PurchaseTB purchase:purchaseList){
			newPrice = purchase.getPrice_purchase();
		}
		
		
		return newPrice;
	}
	
	/**
	 * @version 1.0
	 * @author gee
	 * @see 统计现有库存
	 */
	public int sumGoodsNumberById(int id){
		int sumNum = 0;
		String sql = "select sum("+PurchaseTB.RESERVE_PURCHASE+") as sumNum from "+PurchaseTB.TABLE_NAME+" where " + PurchaseTB.GOODSID_PURCHASE + "="+id
		+ " and " + PurchaseTB.ISCANCEL_PURCHASE + "=false" 
		+ " and " + PurchaseTB.ISCONFIRM_PURCHASE + "=true";
		
		ResultSet result = this.databaseConnection.executeQuery(conn, sql);
		try {
			if(result.next()){
				sumNum = result.getInt("sumNum");
			}else{
				sumNum = -1;
				throw new SQLException(); 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sumNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int sumGoodsNumberById(int id,int warehouseid){
		int sumNum = 0;
		String sql = "select sum("+PurchaseTB.RESERVE_PURCHASE+") as sumNum from "+PurchaseTB.TABLE_NAME+" where " 
		+ PurchaseTB.GOODSID_PURCHASE + "="+id
		+ " and " + PurchaseTB.WAREHOUSEID_PURCHASE + "="+warehouseid
		+ " and " + PurchaseTB.ISCANCEL_PURCHASE + "=false and " + PurchaseTB.RESERVE_PURCHASE +"!=0";
		
		ResultSet result = this.databaseConnection.executeQuery(conn, sql);
		try {
			if(result.next()){
				sumNum = result.getInt("sumNum");
			}else{
				sumNum = -1;
				throw new SQLException(); 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sumNum;
	}
	
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public double sumCountGoodsNumberById(int id){
		double sumNum = 0;
		
		String sql = "select sum("+PurchaseTB.RESERVE_PURCHASE+"*"+PurchaseTB.PRICE_PURCHASE+") as sumNum from "+PurchaseTB.TABLE_NAME
		+" where " + PurchaseTB.GOODSID_PURCHASE + "="+id
		+ " and " + PurchaseTB.ISCANCEL_PURCHASE + "=false" 
		+ " and " + PurchaseTB.ISCONFIRM_PURCHASE + "=true";
		
		ResultSet result = this.databaseConnection.executeQuery(conn, sql);
		try {
			if(result.next()){
				sumNum = result.getDouble("sumNum");
			}else{
				sumNum = -1;
				throw new SQLException(); 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sumNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public double averageGoodsyId(String id){
		double average = 0;
		SqlMachine sqlMachine = new SqlMachine(conn);
		average = sqlMachine.avgFieldForDouble(PurchaseTB.PRICE_PURCHASE,PurchaseTB.GOODSID_PURCHASE,id, PurchaseTB.TABLE_NAME);
		
		return average;
	}
	
	
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean upadtePurchase(PurchaseTB purchase){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(PurchaseTB.RESERVE_PURCHASE, purchase.getReserve_purchase());
		
		
		
		return sqlMachine.updateRow(PurchaseTB.TABLE_NAME, updateMap, PurchaseTB.ID_PURCHASE, purchase.getId_purchase());
	}
	
	
	public List<PurchaseTB> selectPurchase(int beginPageNum,int pageSize){
		
		List<PurchaseTB> purchaseList = null;
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME  + " ORDER BY " + PurchaseTB.DATE_PURCHASE + " DESC "
		+" limit "+beginPageNum+","+pageSize		; 
		purchaseList = selectListBySql(sql);
		
		return purchaseList;
	}
	
	public PurchaseTB getPurchaseById(int id){
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME + " where " + PurchaseTB.ID_PURCHASE + "=" + id;
		PurchaseTB purchase = new PurchaseTB();
		List<PurchaseTB> purchaseList = selectListBySql(sql);
		purchase = purchaseList.get(0);
		return purchase;
	}
	
	public boolean cancle(int id){
	
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(PurchaseTB.ISCANCEL_PURCHASE, true);
		updateMap.put(PurchaseTB.RESERVE_PURCHASE, 0);
		//sqlMachine.updateRow(SupplierTB.TABLE_NAME, updateMap, PurchaseTB.ID_PURCHASE, id);
		
		return sqlMachine.updateRow(PurchaseTB.TABLE_NAME, updateMap, PurchaseTB.ID_PURCHASE, id);
		
		
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxIdByEmail(String email){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(PurchaseTB.ID_PURCHASE, PurchaseTB.TABLE_NAME, PurchaseTB.USEREMAIL_PURCHASE, email);
		return maxId;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + PurchaseTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int sumNumberByPurchaselistId(int id){
		
		int countNum = 0;
		String sql = "select sum("+PurchaseTB.NUMBER_PURCHASE+") as countNum from " + PurchaseTB.TABLE_NAME 
		+ " WHERE " + PurchaseTB.PURCHASELISTID_PURCHASE + "=" + id ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					countNum = resultSet.getInt("countNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return countNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int sumNumberByBackreceiptlistId(int id){
		
		int countNum = 0;
		String sql = "select sum("+PurchaseTB.NUMBER_PURCHASE+") as countNum from " + PurchaseTB.TABLE_NAME 
		+ " WHERE " + PurchaseTB.BACKRECEIPTLISTID_PURCHASE + "=" + id ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					countNum = resultSet.getInt("countNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return countNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int sumReserveByPurchaselistId(int id){
		
		int countNum = 0;
		String sql = "select sum("+PurchaseTB.RESERVE_PURCHASE+") as countNum from " + PurchaseTB.TABLE_NAME 
		+ " WHERE " + PurchaseTB.PURCHASELISTID_PURCHASE + "=" + id ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					countNum = resultSet.getInt("countNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return countNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int sumReserveByBackreceiptlistId(int id){
		
		int countNum = 0;
		String sql = "select sum("+PurchaseTB.RESERVE_PURCHASE+") as countNum from " + PurchaseTB.TABLE_NAME 
		+ " WHERE " + PurchaseTB.BACKRECEIPTLISTID_PURCHASE + "=" + id ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					countNum = resultSet.getInt("countNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return countNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<PurchaseTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		
		
		String sql = "select * from "+PurchaseTB.TABLE_NAME+" WHERE  "
		+PurchaseTB.ID_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.CNSPELL_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.FWCNSPELL_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.GOODSNAME_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.SUPPLIERID_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.USERNAME_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.WAREHOUSENAME_PURCHASE+" LIKE '%"+searchStr+"%'"
		+ " ORDER BY " + PurchaseTB.DATE_PURCHASE + " DESC "
		+" limit "+beginPageNum+","+pageSize; 
		List<PurchaseTB> purchaselist = null;
		purchaselist = selectListBySql(sql);
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		return purchaselist;
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + PurchaseTB.TABLE_NAME +" WHERE  "
		+PurchaseTB.ID_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.CNSPELL_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.FWCNSPELL_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.GOODSNAME_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.SUPPLIERID_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.USERNAME_PURCHASE+" LIKE '%"+searchStr+"%'"
		+" OR "+PurchaseTB.WAREHOUSENAME_PURCHASE+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public Map<String,String> getGoodsWarehouseInfo(int goodsid){
		
		LinkedHashMap<String,String> warehouseMap = new LinkedHashMap<String,String>();
		
		String sql = "SELECT SUM("+PurchaseTB.RESERVE_PURCHASE+") AS "+PurchaseTB.RESERVE_PURCHASE+","+PurchaseTB.WAREHOUSEID_PURCHASE+","+PurchaseTB.WAREHOUSENAME_PURCHASE+" FROM "+PurchaseTB.TABLE_NAME+" WHERE "
		+PurchaseTB.GOODSID_PURCHASE+" = "+goodsid
		+ " and " + PurchaseTB.ISCANCEL_PURCHASE + "=false " 
		+ " and " + PurchaseTB.ISCONFIRM_PURCHASE + "=true "
		
		+" GROUP BY "
		
		+PurchaseTB.WAREHOUSEID_PURCHASE+"";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					String warehousename = resultSet.getString(PurchaseTB.WAREHOUSENAME_PURCHASE);
					warehouseMap.put(resultSet.getInt(PurchaseTB.WAREHOUSEID_PURCHASE)+","+warehousename, warehousename+"/"+resultSet.getString(PurchaseTB.RESERVE_PURCHASE));
					
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return warehouseMap;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getGoodsReserve(int warehouseid,int goodsid){
		int goodsNumber = 0;
		
		
		String sql = "SELECT SUM("+PurchaseTB.RESERVE_PURCHASE+") AS "+PurchaseTB.RESERVE_PURCHASE +" FROM "+PurchaseTB.TABLE_NAME
		+" WHERE "+PurchaseTB.WAREHOUSEID_PURCHASE+" = "+warehouseid
		+" AND "
		+ PurchaseTB.GOODSID_PURCHASE+"="+goodsid
		+ " and " + PurchaseTB.ISCANCEL_PURCHASE + "=false " 
		+ " and " + PurchaseTB.ISCONFIRM_PURCHASE + "=true ";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					goodsNumber = resultSet.getInt(PurchaseTB.RESERVE_PURCHASE);
					//warehouseMap.put(resultSet.getInt(PurchaseTB.WAREHOUSEID_PURCHASE)+","+warehousename, warehousename+"/"+resultSet.getString(PurchaseTB.RESERVE_PURCHASE));
					
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return goodsNumber;
	}
	
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public Map<Integer,List<PurchaseTB>> getWarehouseInfoMapList(List<GoodsTB> goodsList){
		
		
		if(goodsList.size() <=0){
			return null;
		}
		
		StringBuffer inStr = new StringBuffer();
		inStr.append("(");
		
		boolean isFirst = true;
		for(GoodsTB goods:goodsList){
			if(isFirst){
				isFirst = false;
				inStr.append("" + goods.getId_goods());
			}else{
				inStr.append("," + goods.getId_goods());
			}
			
		}
		inStr.append(")");
		String sql = "SELECT SUM("+PurchaseTB.RESERVE_PURCHASE+") AS "+PurchaseTB.RESERVE_PURCHASE

		+","+PurchaseTB.DATE_PURCHASE
		+","+PurchaseTB.FWCNSPELL_PURCHASE
		+","+PurchaseTB.GOODSID_PURCHASE
		+","+PurchaseTB.GOODSNAME_PURCHASE
		+","+PurchaseTB.GOODUNIT_PURCHASE 
		+","+PurchaseTB.ID_PURCHASE
		+","+PurchaseTB.NUMBER_PURCHASE
		+","+PurchaseTB.PRICE_PURCHASE
		+","+PurchaseTB.TOTAL_PURCHASE
		+","+PurchaseTB.REMARK_PURCHASE
		+","+PurchaseTB.SUPPLIERID_PURCHASE
		+","+PurchaseTB.SUPPLIERNAME_PURCHASE
		+","+PurchaseTB.USEREMAIL_PURCHASE
		+","+PurchaseTB.USERNAME_PURCHASE
		+","+PurchaseTB.WAREHOUSEID_PURCHASE
		+","+PurchaseTB.WAREHOUSENAME_PURCHASE
		+","+PurchaseTB.ISCANCEL_PURCHASE
		+","+PurchaseTB.ISCONFIRM_PURCHASE
		+","+PurchaseTB.PURCHASELISTID_PURCHASE
		+","+PurchaseTB.BACKRECEIPTLISTID_PURCHASE
		+","+PurchaseTB.STOCK_PURCHASE
		
		+" FROM "+PurchaseTB.TABLE_NAME 
		+ " WHERE "
		+ PurchaseTB.GOODSID_PURCHASE +" IN " + inStr.toString()
		+ " GROUP BY "+PurchaseTB.GOODSID_PURCHASE+"," + PurchaseTB.WAREHOUSEID_PURCHASE;
		
		List<PurchaseTB> purchaseList = selectListBySql(sql);
		
		
		Map<Integer,List<PurchaseTB>> purchaseListMap = new  HashMap<Integer,List<PurchaseTB>>();
		
		List<PurchaseTB> purchaseList2 = new ArrayList<PurchaseTB>();
		int lastId = -1;
		
		for(PurchaseTB purchase:purchaseList){
			
			
			
			if(lastId != -1 && lastId != purchase.getGoodsid_purchase()){
				purchaseListMap.put(lastId,purchaseList2);	
				
				purchaseList2 = new ArrayList<PurchaseTB>();
				purchaseList2.add(purchase);
				lastId = purchase.getGoodsid_purchase();
				
			}else if(lastId != purchase.getGoodsid_purchase()){
				
				purchaseList2 = new ArrayList<PurchaseTB>();
				purchaseList2.add(purchase);
				lastId = purchase.getGoodsid_purchase();
				
			}else if(lastId == purchase.getGoodsid_purchase()){
				
				purchaseList2.add(purchase);
				lastId = purchase.getGoodsid_purchase();
			}
		}
		purchaseListMap.put(lastId,purchaseList2);	
		
		
		return purchaseListMap;
	}
	
	public PurchaseTB getOldestPurchaseByGoodsid(int goodsid,int warehouseid){
		PurchaseTB purchase = null;
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME 
		+ " where " 
		+ PurchaseTB.GOODSID_PURCHASE +"="+ goodsid + " and "
		+ PurchaseTB.WAREHOUSEID_PURCHASE +"="+ warehouseid 
		+ " and " + PurchaseTB.ISCANCEL_PURCHASE + "=false " 
		+ " and " + PurchaseTB.ISCONFIRM_PURCHASE + "=true "
		+ " and " + PurchaseTB.RESERVE_PURCHASE +">0"
		+ " ORDER BY " + PurchaseTB.DATE_PURCHASE 
		+" limit "+0+","+1;
		
		List<PurchaseTB> purchaseList = selectListBySql(sql);
		if(!(purchaseList.size() == 0)){
			purchase = purchaseList.get(0);
		}
		
		return purchase;
	}
	
	public boolean reserves(ReceiptTB receipt){  //业务逻辑
		
		int warehouseid = receipt.getWarehouseid_receipt();
		int goodsid = receipt.getGoodsid_receipt();
		int number = receipt.getNumber_receipt();
		
		
		
		int goodsnumber = sumGoodsNumberById(goodsid,warehouseid);
		
		if(goodsnumber == 0){ //如果没有查到相关数量或库存数量为0
			return false;
		}
		
		if(number > goodsnumber){  //如果输出大于库存数量
			return false;
		}
		
		if(!recursionReserves(receipt)){ //递归减掉进仓的数量
			return false;
		}
		
		return true;
	}
	
	
	public boolean recursionReserves(ReceiptTB receipt){
		
		int warehouseid = receipt.getWarehouseid_receipt();
		int goodsid = receipt.getGoodsid_receipt();
		int number = receipt.getNumber_receipt();
		
		
		PurchaseTB purchase = getOldestPurchaseByGoodsid(goodsid,warehouseid);
		
		ReceiptrecordOS receiptrecordOS = new ReceiptrecordOS(conn);
		
		ReceiptrecordTB receiptrecord = new ReceiptrecordTB();
		
		if(purchase.getReserve_purchase() < number){ //如果这次进仓数量不足.先减再本次再找另一次进仓数据再减
			number = number - purchase.getReserve_purchase();
			
			
			//记录每一次出仓单
			receiptrecord.setGoodsid_receiptrecord(purchase.getGoodsid_purchase());
			receiptrecord.setGoodsname_receiptrecord(purchase.getGoodsname_purchase());
			receiptrecord.setGoodunit_receiptrecord(purchase.getGoodunit_purchase());
			receiptrecord.setNumber_receiptrecord(purchase.getReserve_purchase());  //记录出仓数量
			receiptrecord.setPrice_receiptrecord(purchase.getPrice_purchase());
			receiptrecord.setPurchaseid_receiptrecord(purchase.getId_purchase());
			receiptrecord.setReceiptid_receiptrecord(receipt.getId_receipt());
			receiptrecord.setReceiptlistid_receiptrecord(receipt.getReceiptlistid_receipt());
			receiptrecord.setUseremail_receiptrecord("SYSTEM");
			receiptrecord.setUsername_receiptrecord("SYSTEM");
			receiptrecord.setWarehouseid_receiptrecord(purchase.getWarehouseid_purchase());
			receiptrecord.setWarehousename_receiptrecord(purchase.getWarehousename_purchase());
			//*/
			//
			double profit = (receipt.getPrice_receipt() - purchase.getPrice_purchase()) * receipt.getNumber_receipt();
			receiptrecord.setProfit_receiptrecord(profit + receiptrecord.getProfit_receiptrecord());
			//*/
			
			purchase.setReserve_purchase(0); //把所以库存减除后值为0
			
			if(!upadtePurchase(purchase)){
				return false;
			}
			receipt.setNumber_receipt(number);
			recursionReserves(receipt);
			
		}else{
			int reserve_purchase = purchase.getReserve_purchase() - number;
			purchase.setReserve_purchase(reserve_purchase);
			
			
			//记录每一次出仓单
			receiptrecord.setGoodsid_receiptrecord(purchase.getGoodsid_purchase());
			receiptrecord.setGoodsname_receiptrecord(purchase.getGoodsname_purchase());
			receiptrecord.setGoodunit_receiptrecord(purchase.getGoodunit_purchase());
			receiptrecord.setNumber_receiptrecord(number);   //记录出仓数量
			receiptrecord.setPurchaseid_receiptrecord(purchase.getId_purchase());
			receiptrecord.setPrice_receiptrecord(purchase.getPrice_purchase());
			receiptrecord.setReceiptid_receiptrecord(receipt.getId_receipt());
			receiptrecord.setReceiptlistid_receiptrecord(receipt.getReceiptlistid_receipt());
			receiptrecord.setUseremail_receiptrecord("SYSTEM");
			receiptrecord.setUsername_receiptrecord("SYSTEM");
			receiptrecord.setWarehouseid_receiptrecord(purchase.getWarehouseid_purchase());
			receiptrecord.setWarehousename_receiptrecord(purchase.getWarehousename_purchase());
			//*/
			
			//
			double profit = (receipt.getPrice_receipt() - purchase.getPrice_purchase()) * receipt.getNumber_receipt();
			receiptrecord.setProfit_receiptrecord(profit + receiptrecord.getProfit_receiptrecord());
			//*/
			
			if(!upadtePurchase(purchase)){
				return false;
			}
		}
		
		if(!receiptrecordOS.insertReceiptrecord(receiptrecord)){
			return false;
		}
		//*/
		return true;
	}
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean changeWarehouse(int goodsid,int number,int warehouseid,int targetwarehouseid){
		
		//int warehouseid = receipt.getWarehouseid_receipt();
		//int goodsid = receipt.getGoodsid_receipt();
		//int number = receipt.getNumber_receipt();
		
		PurchaseTB purchase = getOldestPurchaseByGoodsid(goodsid,warehouseid);
		WarehouseOS warehouseOS = new WarehouseOS(conn);
		
		purchase.setPurchaselistid_purchase(-1);//去除进货单号
		
		//如果这次进仓数量不足.先减再本次再找另一次进仓数据再减
		if(purchase.getReserve_purchase() < number){ 
			number = number - purchase.getReserve_purchase();
			
			int purchaseNum = purchase.getReserve_purchase();
			
			
			purchase.setReserve_purchase(0); //把所以库存减除后值为0
			
			
			if(!upadtePurchase(purchase)){//清空这次进仓产品
				return false;
			}
			
			//添加新的进仓
			purchase.setUsername_purchase("转仓");
			purchase.setUseremail_purchase("转仓");
			purchase.setNumber_purchase(purchaseNum);
			purchase.setWarehouseid_purchase(targetwarehouseid);
			WarehouseTB warehouse = warehouseOS.getWarehouseById(targetwarehouseid);
			purchase.setWarehousename_purchase(warehouse.getName_warehouse());
			
			if(!insert(purchase,true,true)){
				return false;
			}
			changeWarehouse(goodsid,number,warehouseid,targetwarehouseid);
			
		}else{
			int reserve_purchase = purchase.getReserve_purchase() - number;
			purchase.setReserve_purchase(reserve_purchase);
			
			if(!upadtePurchase(purchase)){
				return false;
			}
			
			//添加新的进仓
			purchase.setUsername_purchase("SYSTEM");
			purchase.setUseremail_purchase("SYSTEM");
			purchase.setNumber_purchase(number);
			purchase.setWarehouseid_purchase(targetwarehouseid);
			WarehouseTB warehouse = warehouseOS.getWarehouseById(targetwarehouseid);
			purchase.setWarehousename_purchase(warehouse.getName_warehouse());
			
			if(!insert(purchase,true,true)){
				return false;
			}
		}
		//*/
		return true;
	}
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean cancel(int id,int number){
		SqlMachine sqlMachine = new SqlMachine(conn);
		
		PurchaseTB purchase = getPurchaseById(id);
		
		
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		int reserve = purchase.getReserve_purchase();
		reserve += number;
		
		updateMap.put(PurchaseTB.RESERVE_PURCHASE, reserve);
		
		return sqlMachine.updateRow(PurchaseTB.TABLE_NAME, updateMap, PurchaseTB.ID_PURCHASE, id);
	}
	
	
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean cancel(List<ReceiptrecordTB> receiptrecordlist){
		
		for(ReceiptrecordTB receiptrecord : receiptrecordlist){
			if(!cancel(receiptrecord.getPurchaseid_receiptrecord(),receiptrecord.getNumber_receiptrecord())){
				return false;
			}
			if(!updateGoodsAttribute(receiptrecord.getGoodsid_receiptrecord())){
				return false;
			}
		}
		
		
		
		return true;
	}
	
	/**
	 * @version 1.0
	 * @throws SQLException 
	 * @see 
	 */
	public List<PurchaseTB> selectAll(){
		
		List<PurchaseTB> list = null;
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME
		+ " ORDER BY " + PurchaseTB.ID_PURCHASE;
		
		list = selectListBySql(sql);
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public Map<Integer,List<PurchaseTB>> getMapList(){
		
		List<PurchaseTB> purchaseList = selectAll();
		Map<Integer,List<PurchaseTB>> purchaseListMap = new  HashMap<Integer,List<PurchaseTB>>();
		
		List<PurchaseTB> purchaseList2 = new ArrayList<PurchaseTB>();
		int lastId = -1;
		
		for(PurchaseTB purchase:purchaseList){
			
			
			
			if(lastId != -1 && lastId != purchase.getId_purchase()){
				purchaseListMap.put(lastId,purchaseList);	
				
				purchaseList2 = new ArrayList<PurchaseTB>();
				purchaseList2.add(purchase);
				lastId = purchase.getId_purchase();
				
			}else if(lastId != purchase.getId_purchase()){
				
				purchaseList2 = new ArrayList<PurchaseTB>();
				purchaseList2.add(purchase);
				lastId = purchase.getId_purchase();
				
			}else if(lastId == purchase.getId_purchase()){
				
				purchaseList2.add(purchase);
				lastId = purchase.getId_purchase();
			}
			
			
			
			
			
		}
		
		return purchaseListMap;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public Map<Integer,PurchaseTB> getMap(){
		
		List<PurchaseTB> purchaseList = selectAll();
		Map<Integer,PurchaseTB> purchaseMap = new  HashMap<Integer,PurchaseTB>();
		
	
		
		for(PurchaseTB purchase:purchaseList){
			
			purchaseMap.put(purchase.getId_purchase(), purchase);
			
			
		}
		
		return purchaseMap;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public double getTotal(int day){
		
		int num = 0;
		String sql = "select SUM("+PurchaseTB.TOTAL_PURCHASE+") as num from " + PurchaseTB.TABLE_NAME 
		+ " WHERE "
	 + PurchaseTB.ISCANCEL_PURCHASE+ "=false"
		//+ " limit "+beginPageNum+","+pageSize 
		
		+ " AND " + PurchaseTB.DATE_PURCHASE +">= DATE_ADD(NOW(), INTERVAL "+day+" DAY)";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					num = resultSet.getInt("num");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return num;
	}
	
public List<PurchaseTB> purchaseInfoToReceiptList(String purchaseInfoN,int purchaselistId,int warehouseid_purchase,String warehousename_purchase,OperatorTB operator){
		
		purchaseInfoN = purchaseInfoN.replace("|,", "|");
		List<PurchaseTB> list = new ArrayList<PurchaseTB>();
	
		
		String[] arry = purchaseInfoN.split("\\|");  //里面的参数第一个并不是String，而是regex，是正则式。符号“|”在正则式需要转义才能用，不然就是表示“或”的意思。用"\\|"来就可以了。

		for(String info:arry){
			String[] receiptN = info.split(",");
			
			PurchaseTB obj = new PurchaseTB();
			
			
			//"ii,goodsname,wharehouseid,warehousename,number,price,remark"; 
			obj.setGoodsid_purchase(Integer.parseInt(receiptN[0]));
			obj.setGoodsname_purchase(receiptN[1]);
			
			obj.setWarehouseid_purchase(warehouseid_purchase);
			obj.setWarehousename_purchase(warehousename_purchase);
			obj.setNumber_purchase(Integer.parseInt(receiptN[2])); 
			obj.setGoodunit_purchase(receiptN[3]);
			
			obj.setPrice_purchase(Double.parseDouble(receiptN[4]));
			
			obj.setCnnamediply("");
			obj.setCnspell_purchase("");
			obj.setFwcnspell_purchase("");
			obj.setGoodunit_purchase("");   
			
			
			obj.setPurchaselistid_purchase(purchaselistId);
			obj.setRemark_purchase(receiptN[5]);
			obj.setReserve_purchase(obj.getNumber_purchase());
			obj.setSupplierid_purchase(0);
			obj.setSuppliername_purchase("");
			obj.setTotal_purchase(obj.getNumber_purchase() * obj.getPrice_purchase());
			obj.setUseremail_purchase(operator.getEmail_operator());
			obj.setUsername_purchase(operator.getName_operator());
			
			GoodsOS goodsOS = new GoodsOS(conn);
			GoodsTB goods = goodsOS.getGoodsById(obj.getGoodsid_purchase()); 
			
			obj.setStock_purchase(goods.getNumber_goods());
			
			list.add(obj);
		}
		
		
		return list;
	}

	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<PurchaseTB> getListByPurchaselistId(int id){
	
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME + " where " + PurchaseTB.PURCHASELISTID_PURCHASE + "=" + id;
		List<PurchaseTB> list = selectListBySql(sql);
		
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<PurchaseTB> getListByBackreceiptlistId(int id){
		
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME + " where " + PurchaseTB.BACKRECEIPTLISTID_PURCHASE + "=" + id;
		List<PurchaseTB> list = selectListBySql(sql);
		
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public Map<Integer,List<PurchaseTB>> getListMap(String inStr){
		if(inStr.length() < 3){
			return null;
		}
		
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME  
			+ " where " + PurchaseTB.PURCHASELISTID_PURCHASE + " IN "+inStr+""; 
		
		List<PurchaseTB> list = new ArrayList<PurchaseTB>();
		list = selectListBySql(sql);
	
		
		
		Map<Integer,List<PurchaseTB>> valueMap = new HashMap<Integer,List<PurchaseTB>>();
		
		List<PurchaseTB> valueList = new ArrayList<PurchaseTB>();
		int value = -1;
		//int lastFlowValue = -1;
		
		for(PurchaseTB obj:list){
		//	
			if(value != -1 && value != obj.getPurchaselistid_purchase()){
				valueMap.put(value,valueList);
				valueList = new ArrayList<PurchaseTB>();
				valueList.add(obj);
				value = obj.getPurchaselistid_purchase();
			}else if(value != obj.getPurchaselistid_purchase()){
				valueList = new ArrayList<PurchaseTB>();
				valueList.add(obj);
				value = obj.getPurchaselistid_purchase();
			}else if(value == obj.getPurchaselistid_purchase()){
				valueList.add(obj);
				value = obj.getPurchaselistid_purchase();
			}
			if(obj.getPurchaselistid_purchase() == -1){
				valueMap.put(value,valueList);
				valueList = new ArrayList<PurchaseTB>();
				valueList.add(obj);
				value = obj.getPurchaselistid_purchase();
			}
		}
		
		valueMap.put(value,valueList);//修改正最后一次加入
		
		return valueMap;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public Map<Integer,List<PurchaseTB>> getListMapByBackreceiptlistid(String inStr){
		if(inStr.length() < 3){
			return null;
		}
		
		String sql = "SELECT * FROM " + PurchaseTB.TABLE_NAME  
			+ " where " + PurchaseTB.BACKRECEIPTLISTID_PURCHASE + " IN "+inStr+""; 
		
		List<PurchaseTB> list = new ArrayList<PurchaseTB>();
		list = selectListBySql(sql);
	
		
		 
		Map<Integer,List<PurchaseTB>> valueMap = new HashMap<Integer,List<PurchaseTB>>();
		
		List<PurchaseTB> valueList = new ArrayList<PurchaseTB>();
		int value = -1;
		//int lastFlowValue = -1;
		
		for(PurchaseTB obj:list){
		//	
			if(value != -1 && value != obj.getBackreceiptlistid_purchase()){
				valueMap.put(value,valueList);
				valueList = new ArrayList<PurchaseTB>();
				valueList.add(obj);
				value = obj.getBackreceiptlistid_purchase();
			}else if(value != obj.getBackreceiptlistid_purchase()){
				valueList = new ArrayList<PurchaseTB>();
				valueList.add(obj);
				value = obj.getBackreceiptlistid_purchase();
			}else if(value == obj.getBackreceiptlistid_purchase()){
				valueList.add(obj);
				value = obj.getBackreceiptlistid_purchase();
			}
			if(obj.getBackreceiptlistid_purchase() == -1){
				valueMap.put(value,valueList);
				valueList = new ArrayList<PurchaseTB>();
				valueList.add(obj);
				value = obj.getBackreceiptlistid_purchase();
			}
		}
		
		valueMap.put(value,valueList);//修改正最后一次加入
		
		return valueMap;
	}
}
